import cn.edu.dgut.Javabean_object.Person;
import cn.edu.dgut.Javabean_object.userinfo;
import cn.edu.dgut.function.readfile;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet( "/check-login")
public class passwordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EntityManager em = (EntityManager) request.getAttribute("_entity.manager");
        int flag =1;
        String user = request.getParameter("user_name");
        String password=request.getParameter("password");
        if(em.find(Person.class,user)==null)
            flag=0;
        else if(em.find(Person.class,user)!=null ){
          Person userer=em.find(Person.class,user);
          if(userer.getPassword()!=password) flag=2;
        }

  /*      userinfo[] users = new readfile().getUers();



        for (int i = 0; i < users.length; i++) {
            if (users[i].getUser_number()!= null) {
                if (users[i].getUser_number().equals(user) && users[i].getPassword().equals(password)) {
                    flag = 1;
                    break;
                }
            }
        }
       if(flag==0)
       {

           flag=2;
           for (int i = 0; i < users.length; i++) {
               if (users[i].getUser_number() != null) {
                   if (users[i].getUser_number().equals(user)) {
                       flag=0;
                       break;
                   }
               }
           }
       }
        */
        response.setContentType("application/json,charset=utf-8");
        String strJson = "{\"flag\":\""+flag+"\"}";
        response.getWriter().println(strJson);
    }
}
